<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.courseName"
        clearable
        style="width: 200px;"
        placeholder="请输入课程名称"
      />
      <el-button type="primary" icon="el-icon-search" @click="getList">查找</el-button>
      <el-button type="primary" icon="el-icon-edit" @click="addCourse">新建课程</el-button>
      <el-button
        :disabled="multipleSelection.length===0"
        type="danger"
        icon="el-icon-delete"
        @click="handleDeleteAll"
      >批量删除</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column align="center" label="课程ID" prop="courseId" />
      <el-table-column align="center" label="课程名称" prop="courseName" />
      <el-table-column align="center" label="限制" prop="courseLimit">
        <template slot-scope="scope">
          <div>{{ scope.row.courseLimit === 1 ? '免费' : '会员免费' }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="视频数量" prop="videoCount">
        <template slot-scope="scope">
          <div>{{ scope.row.videoCount ? scope.row.videoCount : '0' }}</div>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="状态">
        <template slot-scope="scope">
          <el-badge :type="scope.row.status===1?'success':'danger'" is-dot class="item">{{ scope.row.status===1 ? '显示' : '隐藏' }}</el-badge>
        </template>
      </el-table-column>-->
      <!-- <el-table-column align="center" label="排序" prop="sortNo" /> -->
      <el-table-column align="center" label="播放量" prop="viewCount" />

      <el-table-column align="center" label="创建时间" prop="created" />

      <el-table-column align="center" label="操作" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="getDetail(scope.row)">编辑</el-button>
          <el-button
            v-permission="['POST /admin/category/delete']"
            type="danger"
            size="mini"
            @click="handleDeleteAll(scope.row.courseId)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.size"
      @pagination="getList"
    />
  </div>
</template>

<style scoped>
.filter-item {
  margin-left: 100px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 145px;
  height: 145px;
  display: block;
}
</style>

<script>
import { getCourseList, delCourse } from '@/api/course'
import { uploadPath } from '@/api/storage'
import { getToken } from '@/utils/auth'
import Pagination from '@/components/Pagination'
export default {
  name: 'Category',
  components: { Pagination },
  data() {
    return {
      uploadPath,
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        courseName: null,
        index: 0,
        size: 10
      },
      dialogFormVisible: false,
      dataForm: {},
      multipleSelection: []
    }
  },
  computed: {
    headers() {
      return {
        'X-Litemall-Admin-Token': getToken()
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(e) {
      if (e && e.page) {
        this.listQuery.index = e.page - 1
      }
      this.listLoading = true
      getCourseList(this.listQuery)
        .then(response => {
          this.list = response.data.data.content
          this.total = response.data.data.totalRecords
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.listLoading = false
        })
    },
    addCourse() {
      this.$router.push({ name: 'courseAdd' })
    },
    resetForm() {
      this.dataForm = {
        categoryId: '',
        category: '',
        showStatus: 1,
        sortNo: ''
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleDeleteAll(courseId) {
      const idList = []
      if (!courseId) {
        this.multipleSelection.forEach(item => {
          idList.push(item.courseId)
        })
      } else {
        idList.push(courseId)
      }

      delCourse({ idList }).then(res => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      })
    },
    getDetail(item) {
      this.$router.push({
        path: '/course/courseEdit',
        query: {
          id: item.courseId
        }
      })
    }
  }
}
</script>
